package fop;

import org.apache.poi.hwpf.converter.WordToFoConverter;
import org.apache.poi.hwpf.converter.WordToFoUtils;
import org.apache.poi.hwpf.usermodel.Picture;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.util.Base64;

/**
 * Word转XML，再转PDF时，图片需要单独处理
 * @author caiwl
 * @date 2020/10/30 10:43
 */
public class WordToFoWithImageConverter extends WordToFoConverter {
    public WordToFoWithImageConverter(Document document) {
        super(document);
    }

    @Override
    protected void processImageWithoutPicturesManager(Element currentBlock, boolean inlined, Picture picture) {
        // 没有图片地址，只能转Base64处理
        String source = "data:" + picture.getMimeType() + ";base64," + Base64.getMimeEncoder().encodeToString(picture.getRawContent());
        // 创建图片fo元素
        Element externalGraphic = foDocumentFacade.createExternalGraphic(source);
        // 设置当前图片属性
        WordToFoUtils.setPictureProperties(picture, externalGraphic);
        // 将图片fo元素加入XML文件
        currentBlock.appendChild(externalGraphic);
    }
}
